﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using T.ApiResults;
using T.Vehicleservice.API.Application.Command.AccidentRecordCommand;

namespace T.Vehicleservice.API.Controllers
{
    /// <summary>
    /// 事故控制器(写)
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AccidentRecordController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;    
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="mediator"></param>
        public AccidentRecordController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加事故
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Create(AccidentRecordCreateCommand command)
        {
            ApiResult<int> result= new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(new AccidentRecordCreateCommand
                {
                    AccidentId = command.AccidentId,
                    AccidentTitle = command.AccidentTitle,
                    LicensePlateNumber = command.LicensePlateNumber,
                    IncidentPerson = command.IncidentPerson,
                    AccidentDate = command.AccidentDate,
                    InsuranceIndemnify = command.InsuranceIndemnify,
                    CompanyLosses = command.CompanyLosses,
                    AccidentDesc = command.AccidentDesc,
                    EconomicLoss = command.EconomicLoss,
                    AccidentRemark = command.AccidentRemark,
                    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,//获取公司Id,
                    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value), //获取用户Id,
                    CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value//获取用户名称
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除事故
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Deleted(AccidentRecordDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(new AccidentRecordDelCommand
                {
                    AccidentId = command.AccidentId,
                    AccidentTitle = command.AccidentTitle,
                    LicensePlateNumber = command.LicensePlateNumber,
                    IncidentPerson = command.IncidentPerson,
                    AccidentDate = command.AccidentDate,
                    InsuranceIndemnify = command.InsuranceIndemnify,
                    CompanyLosses = command.CompanyLosses,
                    AccidentDesc = command.AccidentDesc,
                    EconomicLoss = command.EconomicLoss,
                    AccidentRemark = command.AccidentRemark,
                    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,//获取公司Id,
                    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value), //获取用户Id,
                    DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value//获取用户名称
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改事故
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Update(AccidentRecordUpdCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(new AccidentRecordUpdCommand
                {
                    AccidentId = command.AccidentId,
                    AccidentTitle = command.AccidentTitle,
                    LicensePlateNumber = command.LicensePlateNumber,
                    IncidentPerson = command.IncidentPerson,
                    AccidentDate = command.AccidentDate,
                    InsuranceIndemnify = command.InsuranceIndemnify,
                    CompanyLosses = command.CompanyLosses,
                    AccidentDesc = command.AccidentDesc,
                    EconomicLoss = command.EconomicLoss,
                    AccidentRemark = command.AccidentRemark,
                    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,//获取公司Id,
                    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value), //获取用户Id,
                    UpdatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value//获取用户名称
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 批量删除事故
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> BatchDel([FromQuery]AccidentRecordBatchDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
